﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Library;

namespace OA.Exam
{
    public partial class PapersManage : System.Web.UI.Page
    {
        Exam myExam = new Exam();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindView();
            }
        }
        //绑定试卷列表
        protected void BindView()
        {
            string sql = "select PaperId,PaperName,Score,Status,ExamTime from Exam_Papers where 1=1";
            if (TxtPaperName.Text.Trim() != "")
            {
                sql += " and PaperName like '%" + TxtPaperName.Text.Trim() + "%'";
            }
            DataTable dt = DataAccess.getDataTable(sql);
            GridViewPapers.DataSource = dt.DefaultView;
            GridViewPapers.DataBind();
        }
        //查询
        protected void BtnSearcn_Click(object sender, EventArgs e)
        {
            BindView();
        }
        //新增
        protected void BtnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("PapersMaintain.aspx");
        }

        protected void GridViewPapers_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
            string paperId = GridViewPapers.DataKeys[rowIndex].Value.ToString();
            //修改试卷
            if (e.CommandName == "EditPaper")
            {
                Response.Redirect("PapersMaintain.aspx?paperId=" + paperId);
            }
            //删除试卷
            if (e.CommandName == "DeletePaper")
            {
                if (myExam.DeletePaper(paperId))
                {
                    LabPrompt.Text = "删除成功";
                    BindView();//绑定试卷列表
                }
                else
                {
                    LabPrompt.Text = "删除失败";
                }
            }
            //设置大题
            if (e.CommandName == "SetPaper")
            {
                Response.Redirect("SetPaperQuestions.aspx?paperId=" + paperId);
            }
        }
        //后台处理删除提示
        protected void GridViewPapers_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton LbtnDelete = (LinkButton)e.Row.FindControl("LbtnDelete");
                if (LbtnDelete.Enabled == true)
                {
                    LbtnDelete.Attributes.Add("onclick", "javascript:return confirm('确定要删除该试卷吗？');");
                }
            }
        }
        //开始考试
        protected void BtnBegin_Click(object sender, EventArgs e)
        {
            bool flag = false;
            int mark = 0;
            int i;
            for (i = 0; i < GridViewPapers.Rows.Count; i++)
            {
                string paperId = GridViewPapers.DataKeys[i].Values[0].ToString();
                string status = GridViewPapers.DataKeys[i].Values[1].ToString();
                CheckBox ChbCheck = (CheckBox)GridViewPapers.Rows[i].FindControl("ChbCheck");
                if (ChbCheck.Checked && status == "1")//只有未使用的试卷可以开始考试
                {
                    mark++;
                    flag = myExam.UpdateStatusOfPaper(paperId, "2");
                }
            }
            if (mark == 0)
            {
                LabPrompt.Text = "您还没有选择试卷，或选择试卷的状态不是未使用。";
                return;
            }
            if (flag)
            {
                LabPrompt.Text = "设置成功";
                BindView();
            }
        }
        //结束考试
        protected void BtnEnd_Click(object sender, EventArgs e)
        {
            bool flag = false;
            int mark = 0;
            int i;
            for (i = 0; i < GridViewPapers.Rows.Count; i++)
            {
                string paperId = GridViewPapers.DataKeys[i].Values[0].ToString();
                string status = GridViewPapers.DataKeys[i].Values[1].ToString();
                CheckBox ChbCheck = (CheckBox)GridViewPapers.Rows[i].FindControl("ChbCheck");
                if (ChbCheck.Checked && status == "2")//只有考试中的试卷可以结束考试
                {
                    mark++;
                    flag = myExam.UpdateStatusOfPaper(paperId, "3");
                }
            }
            if (mark == 0)
            {
                LabPrompt.Text = "您还没有选择试卷，或选择试卷的状态不是未使用。";
                return;
            }
            if (flag)
            {
                LabPrompt.Text = "设置成功";
                BindView();
            }
        }
    }
}
